﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Xml.Linq;
using System.Collections.Generic;
using LUStationery.Model;
using LUStationery.DAO;
using LUStationery.Controller;

namespace LUStationery.Controller
{

    public class RequestListControl
    {

        public RequestList getRequestsByUserNameAndStatus(string userID,string status)
        {
            UsersControl ucont = new UsersControl();
            User u = ucont.getUserByUserID(userID);
            RequestList rList = getRequestsByStaffIDAndStatus(u.StaffID, status);
            return rList;
        }

        public RequestList getRequestsByUserNameAndStatusAndRequisitionFormID(string userName, string status, string requisitionFormID)
        {
            UsersControl ucont = new UsersControl();
            User u = ucont.getUserByUserID(userName);
            RequestList rList = getRequestsByUserNameAndStatusAndRequisitionFormID(u.UserName, status, requisitionFormID);
            return rList;
        }


        public RequestList getRequestsByUserName(string userName)
        {
            RequestDAO rDAO = new RequestDAO();
            IDataReader idr = rDAO.getRequestsByUserName(userName);
            return RequestListControl.convertIdrToRequestList(idr);
        }

        public RequestList getRequestsByStatus(string status)
        {
            RequestDAO rDAO = new RequestDAO();
            IDataReader idr = rDAO.getRequestsByStatus(status);
            return RequestListControl.convertIdrToRequestList(idr);
        }

        public RequestList getRequestsByStaffIDAndStatus(string staffID, string status)
        {
            RequestDAO rDAO = new RequestDAO();
            IDataReader idr = rDAO.geRequestsByStaffIDAndStatus(staffID, status);
            return RequestListControl.convertIdrToRequestList(idr);
        }

        public RequestList getRequestsByStaffIDAndStatusAndRequisitionFormID(string staffID, string status, string requisitionFormID)
        {
            RequestDAO rDAO = new RequestDAO();
            IDataReader idr = rDAO.geRequestsByStaffIDAndStatusAndRequisitionFormID(staffID, status, requisitionFormID);
            return RequestListControl.convertIdrToRequestList(idr);
        }

        protected static RequestList convertIdrToRequestList(IDataReader idr)
        {
            RequestList rList = new RequestList();
            while (idr.Read())
            {
                Request request = new Request();
                request.ItemCode = Convert.ToString(idr["item_code"]);
                request.ItemDesc = Convert.ToString(idr["item_description"]);
                request.RequestQty = Convert.ToInt32(idr["request_qty"]);
                request.ActualQty = Convert.ToInt32(idr["actual_qty"]);
                request.Status = Convert.ToString(idr["request_status"]);
                request.RequestDate = Convert.ToString(idr["request_date"]);
                request.RequisitionFormID = Convert.ToString(idr["requisition_form_id"]);
                rList.Add(request);
            }
            return rList;
        }
    }
}